import moment from "moment";
import { Base64 } from "js-base64";

const setLocalStorageSelf = (
  key: string,
  options: object | Array<Object> | string
): void => {
  localStorage.setItem(key, <any>options);
};

const getLocalStorageSelf = (key: string): any => {
  return localStorage.getItem(key);
};

const setSessionStorageSelf = (
  key: string,
  options: object | Array<Object> | string
): void => {
  sessionStorage.setItem(key, <any>options);
};

const getSessionStorageSelf = (key: string): any => {
  return sessionStorage.getItem(key);
};

const storeInfoWithTimeAndFlag = (
  key: string,
  options: object | Array<Object>,
  time?: string,
  updateFlag?: boolean,
  useBeforeTime?: boolean
) => {
  if (!time) {
    time = moment().format("YYYY-MM-DD");
  }
  if (updateFlag === undefined) {
    updateFlag = true;
  }
  if (useBeforeTime === undefined) {
    useBeforeTime = false;
  }
  let beforeInfo = getEncryptionInfo(key);
  if (beforeInfo) {
    if (updateFlag) {
      // 更新数据
      const nowT = moment().format("YYYY-MM-DD");
      localStorage.removeItem(<string>beforeInfo.key);
      if (moment(beforeInfo.passTime).isSameOrBefore(nowT)) {
        //未过期
        if (useBeforeTime) {
          setLocalStorageSelf(
            <string>beforeInfo.key,
            Base64.encode(JSON.stringify(options))
          );
        } else {
          defaultSave(key, time, options);
        }
      } else {
        // 已过期
        defaultSave(key, time, options);
      }
    } else {
      // 不更新数据，直接添加
      defaultSave(key, time, options);
    }
  } else {
    defaultSave(key, time, options);
  }
};

// 默认添加方式
const defaultSave = (
  key: string,
  time: string,
  options: object | Array<object>
) => {
  let saveKey: string =
    key +
    "_" +
    moment().format("DDkkmmSS") +
    "_" +
    Math.ceil(Math.random() * 9 + 1);
  let saveInfo: string = time + "&" + saveKey;
  setLocalStorageSelf(key, Base64.encode(saveInfo));
  setLocalStorageSelf(
    Base64.encode(saveKey),
    Base64.encode(JSON.stringify(options))
  );
};

// 基层加密数据类型
interface resType {
  key: string | undefined;
  passTime: string;
}

// 获取基层加密数据
const getEncryptionInfo = (key: string): resType | undefined => {
  let tempInfo = getLocalStorageSelf(key);
  let lastRes: resType | undefined = undefined;
  if (tempInfo) {
    let tempTime = Base64.decode(tempInfo).split("&")[0];
    let tempKey = Base64.encode(Base64.decode(tempInfo).split("&")[1]);
    lastRes = {
      key: tempKey,
      passTime: tempTime,
    };
  } else {
    console.log("数据不存在！");
  }
  return lastRes;
};

// 获取数据
const getStoreInfoWithKey = (key: string): object | Array<object> => {
  const tempInfo = getLocalStorageSelf(key);
  let lastInfo: object | Array<object> = {};
  if (tempInfo) {
    // let passTime = Base64.decode(tempInfo).split("&")[0];
    let tempKey = Base64.encode(Base64.decode(tempInfo).split("&")[1]);
    lastInfo = JSON.parse(Base64.decode(getLocalStorageSelf(tempKey)));
  } else {
    console.log("暂无存储数据！");
  }
  return lastInfo;
};

export default {
  getStoreInfoWithKey,
  storeInfoWithTimeAndFlag,
};
